package controller;
import java.io.IOException;

import view.VendingMachineView;

import model.VendingMachineModel;


public class VendingMachineController {

  private VendingMachineModel model;
  private VendingMachineView view;

  public VendingMachineController( VendingMachineModel model, VendingMachineView view ){
    this.model = model;
    this.view = view;
  }
  
  public void insertCoin(int value){
    model.insertCoin(value);
  }

  public void buyDrink(){
    view.outputDrink();
    model.buyDrink();
  }
  
  public void returnChange(){
    view.printOutput();
    model.clearCoin();
  }
  
  public void showDrinkPrice(){
    view.printDrinkPrice();
  }
  
  public void showBalance(){
    view.printBalance();
  }

  public void showErrorMessage(){
    view.printErrorMessage();
  }

  public void inputProgress() throws IOException{
    boolean is_loop = true;
    
    while(is_loop){
      switch(view.printInput()){

        case 1:
          insertCoin(10);
          break;

        case 2:
          insertCoin(100);
          break;

        case 3:
          if( model.getCoin() >= model.getDrinkPrice() ){
            buyDrink();
          } else{
            showErrorMessage();
          }
          break;

        case 4:
          is_loop = false;
          returnChange();
          break;

        default:
          break;
      }
      //showBalance();
    }
  }
}